// VBConversions Note: VB project level imports
using System.Diagnostics;
using System;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using Microsoft.VisualBasic;
using System.Data;
using System.Collections.Generic;
using Microsoft.VisualBasic.Compatibility;
using System.Linq;
// End of VB project level imports

using System.Text.RegularExpressions;


namespace 喷码数据生成工具
{
	public partial class frmConversion
	{
		public frmConversion()
		{
			InitializeComponent();
		}
		const float InchAndMM = 25.4F;
		bool MoveDown = false;
		int MoveY;
		int TimCount = 0;
		public void frmConversion_Load(System.Object sender, System.EventArgs e)
		{
			CommFunction.ChangeButtonColor(Btn_Convert);
			Cbo_ConvertMode.Items.Add("毫米转英寸");
			Cbo_ConvertMode.Items.Add("英寸转毫米");
			Cbo_ConvertMode.Items.Add("十六进制转十进制");
			Cbo_ConvertMode.Items.Add("十进制转十六进制");
			Cbo_ConvertMode.Items.Add("十进制转二进制");
			Cbo_ConvertMode.Items.Add("二进制转十进制");
			Cbo_ConvertMode.Items.Add("十进制转VisualBasic十六进制");
			Cbo_ConvertMode.Items.Add("十进制转C#十六进制");
			Cbo_ConvertMode.SelectedIndex = 0;
			TipText.Top = this.ClientRectangle.Height;
			Panel1.BringToFront();
		}
#region 判断是否是数值型数据
		public static bool IsNumber(string value)
		{
			if (value == "0" || string.IsNullOrEmpty(value))
			{
				return true;
			}
			Regex r = new Regex("^[+-]?[0-9]+[.]?[0-9]+$");
			if (value.Length == 1)
			{
				Regex g = new Regex("^[0-9]+$");
				return g.IsMatch(value);
			}
			return r.IsMatch(value);
		}
		public static bool IsBinaryNumber(string value)
		{
			if (value == "0" || string.IsNullOrEmpty(value))
			{
				return true;
			}
			Regex r = new Regex("^[+-]?[0-1]+[.]?[0-1]+$");
			if (value.Length == 1)
			{
				Regex g = new Regex("^[0-1]+$");
				return g.IsMatch(value);
			}
			return r.IsMatch(value);
		}
		public static bool IsHexNumber(string value)
		{
			if (value == "0" || string.IsNullOrEmpty(value))
			{
				return true;
			}
			Regex r = new Regex("^[0x]?[0-9]+[A]?[B]?[C]?[D]?[E]?[F]+$");
			if (value.Length == 1)
			{
				Regex g = new Regex("^[0-1]+$");
				return g.IsMatch(value);
			}
			return r.IsMatch(value);
		}
#endregion
		private void HexadecimalConversion(string value)
		{
			string Tmp = TB_ConvertValue.Text;
			if (string.IsNullOrEmpty(Tmp))
			{
				MessageBox.Show(this, "需转换数值文本不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				return ;
			}
			try
			{
				if (value == ("毫米转英寸"))
				{
					if (true)
					{
						if (IsNumber(Tmp) == false)
						{
							throw (new Exception("转换文本不能含有中文和特殊字符！"));
						}
						TB_Result.Text = System.Convert.ToString(MMsubInch(Tmp).ToString());
					}
				}
				else if (value == "英寸转毫米")
				{
					if (true)
					{
						if (IsNumber(Tmp) == false)
						{
							throw (new Exception("转换文本不能含有中文和特殊字符！"));
						}
						TB_Result.Text = System.Convert.ToString(InchSubMM(Tmp).ToString());
					}
				}
				else if (value == "二进制转十进制")
				{
					if (true)
					{
						if (IsBinaryNumber(Tmp) == false)
						{
							throw (new Exception("转换文本只能含有[0，1，.！]"));
						}
						TB_Result.Text = System.Convert.ToString(BinaryToDecimalSub(Tmp).ToString());
					}
				}
				else if (value == "十进制转二进制")
				{
					if (true)
					{
						if (IsNumber(Tmp) == false)
						{
							throw (new Exception("转换文本不能含有中文和特殊字符！"));
						}
						TB_Result.Text = DecToBin(Tmp);
					}
				}
				else if (value == "十进制转十六进制")
				{
					if (true)
					{
						if (IsNumber(Tmp) == false)
						{
							throw (new Exception("转换文本不能含有中文和特殊字符！"));
						}
						TB_Result.Text = DectoHex(Convert.ToInt32(Tmp));
					}
				}
				else if (value == "十六进制转十进制")
				{
					//   if (IsHexNumber(Tmp) == false) { throw new Exception("转换文本必须为十六进制数据！"); }
					TB_Result.Text = System.Convert.ToString(HexToDec(Tmp).ToString());
				}
				else if (value == "十进制转VisualBasic十六进制")
				{
					TB_Result.Text = GetDecToVBHex(TB_ConvertValue.Text);
				}
				else if (value == "十进制转C#十六进制")
				{
					TB_Result.Text = GetDecToCShapHex(TB_ConvertValue.Text);
				}
				else
				{
					TB_Result.Text = "";
				}
				Clipboard.SetText(TB_Result.Text);
				int TipHeight = 0;
				TipText.AutoSize = true;
				TipText.Text = "转换结果：【" + TB_Result.Text + "】已复制到剪贴板！";
				TipHeight = TipText.Height;
				if (TipText.Width > this.ClientRectangle.Width)
				{
					TipText.AutoSize = false;
					TipText.Width = this.ClientRectangle.Width;
					TipText.Height = TipHeight * 2;
					TipText.Left = 0;
				}
				else
				{
					TipText.AutoSize = true;
					TipText.Left = (int) ((double) (this.ClientRectangle.Width - TipText.Width) / 2);
				}
				MoveY = this.ClientRectangle.Height;
				MoveDown = false;
				TimCount = 0;
				Timer1.Enabled = true;
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		//=============================================转换函数
		private double InchSubMM(string value)
		{
			//英寸转毫米
			try
			{
				return (Convert.ToDouble(value) * 25.4);
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return -1;
			}
		}
		private double MMsubInch(string value)
		{
			//毫米转英寸
			try
			{
				double result = 0;
				result = Convert.ToDouble(value) / 25.4;
				return result;
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return -1;
			}
		}
		private string RGB2HEX(byte R, byte G, byte B)
		{
			//RGB转HEX  1
			return "#" + Convert.ToString(System.Convert.ToByte(Color.FromArgb(R, G, B).ToArgb()), 16).Substring(2);
		}
		private string RGB2HEX(byte A, byte R, byte G, byte B)
		{
			//RGB转HEX  2
			return "#" + Convert.ToString(System.Convert.ToByte(Color.FromArgb(A, R, G, B).ToArgb()), 16).Substring(2);
		}
		private double BinaryToDecimalSub(string value)
		{
			//二进制转十进制
			int a = -1;
			string str1 = "";
			string str2 = "";
			double Sum1 = 0;
			double Sum2 = 0;
			a = value.IndexOf(".");
			if (a > 0)
			{
				str1 = value.Substring(0, a);
				str2 = value.Substring(a + 1);
				for (var i = str1.Length - 1; i >= 0; i--)
				{
					Sum1 += Convert.ToInt32(str1.Substring(System.Convert.ToInt32(i), 1)) * Math.Pow(2, Math.Abs(i - value.Length + 1));
				}
				for (int j = 0; j <= str2.Length - 1; j++)
				{
					Sum2 += Convert.ToInt32(str2.Substring(j, 1)) * Math.Pow(2, - (j + 1));
				}
				return Sum1 + Sum2;
			}
			else
			{
				for (var i = value.Length - 1; i >= 0; i--)
				{
					Sum1 += 1 * Math.Pow(2, Math.Abs(i - value.Length + 1));
				}
				return Sum1;
			}
			//  return null;
		}
		
		private string DecToBin(string value)
		{
			//十进制转二进制
			int a = 0;
			string str1 = "";
			string str2 = "";
			string Result = "";
			string Tmp1 = "";
			string Tmp4 = "";
			int TmpValue1 = 0;
			int TmpValue2 = 0;
			double Tmp2 = 0;
			double Tmp3 = 0;
			a = value.IndexOf(".");
			if (a > 0)
			{
				str1 = value.Substring(0, a);
				str2 = value.Substring(a + 1);
				TmpValue1 = Convert.ToInt32(str1);
				while (TmpValue1 >= 1)
				{
					TmpValue2 = TmpValue1 % 2;
					TmpValue1 = TmpValue1 / 2;
					Tmp1 += TmpValue2.ToString();
				}
				for (int i = Tmp1.Length - 1; i >= 0; i--)
				{
					Result += Tmp1.Substring(i, 1);
				}
				Result += ".";
				Tmp2 = Convert.ToDouble("0." + str2);
				Tmp3 = Convert.ToInt32(str2);
				while (Tmp3 != 0)
				{
					Tmp2 = Tmp2 * 2;
					str2 = Tmp2.ToString();
					a = str2.IndexOf(".");
					if (a > 0)
					{
						str1 = str2.Substring(0, a);
						Tmp4 = str2.Substring(a + 1);
						Tmp2 = Convert.ToDouble("0." + Tmp4);
						Tmp3 = Convert.ToInt32(Tmp4);
						Result += str1;
					}
					else
					{
						Result += "1";
						Tmp3 = 0;
					}
				}
				return Result;
			}
			else
			{
				TmpValue1 = Convert.ToInt32(value);
				while (TmpValue1 >= 1)
				{
					TmpValue2 = TmpValue1 % 2;
					TmpValue1 = TmpValue1 / 2;
					Tmp1 += TmpValue2.ToString();
				}
				for (int i = Tmp1.Length - 1; i >= 0; i--)
				{
					Result += Tmp1.Substring(i, 1);
				}
				return Result;
			}
		}
		//========================================十六进制转换十进制
		private int HexToDec(string value)
		{
			string HexData = "0123456789ABCDEF";
			int TmpValue = 0;
			int sIndex = 0;
			string Tmp = "";
			for (int i = value.Length - 1; i >= 0; i--)
			{
				Tmp = System.Convert.ToString(value.Substring(i, 1).ToUpper());
				sIndex = HexData.IndexOf(Tmp);
				TmpValue += Convert.ToInt32(sIndex * Math.Pow(16, value.Length - 1 - i));
			}
			return TmpValue;
		}
		private string DectoHex(int value)
		{
			string x = "";
			if (value < 16)
			{
				x = GetHexString(value);
			}
			else
			{
				int c = 0;
				int n = value;
				int temp = value;
				string tmp = "";
				string s = "";
				while (n > 0)
				{
					c = n % 16;
					n = n / 16;
					tmp += GetHexString(c);
				}
				for (int i = tmp.Length - 1; i >= 0; i--)
				{
					s += tmp.Substring(i, 1);
				}
				x = s;
			}
			return x;
		}
		private string GetHexString(int value)
		{
			switch (value)
			{
				case 10:
					return "A";
				case 11:
					return "B";
				case 12:
					return "C";
				case 13:
					return "D";
				case 14:
					return "E";
				case 15:
					return "F";
				default:
					return value.ToString();
			}
		}
		private string GetDecToVBHex(string value)
		{
			if (IsNumber(value) == false)
			{
				Interaction.MsgBox("转换数值必须是数值型数据！", MsgBoxStyle.Information, "提示");
			}
			return "&H" + DectoHex(Convert.ToInt32(value));
		}
		private string GetDecToCShapHex(string value)
		{
			if (IsNumber(value) == false)
			{
				Interaction.MsgBox("转换数值必须是数值型数据！", MsgBoxStyle.Information, "提示");
			}
			return "0x" + DectoHex(Convert.ToInt32(value));
		}
		//=============================================转换函数
		
		public void Btn_Convert_Click(System.Object sender, System.EventArgs e)
		{
			HexadecimalConversion(Cbo_ConvertMode.Text);
		}
		
		public void TB_Result_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			e.Handled = true;
		}
		
		public void TB_ConvertValue_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (Strings.Asc(e.KeyChar) == 13)
			{
				HexadecimalConversion(Cbo_ConvertMode.Text);
			}
		}
		
		public void TipText_Click(System.Object sender, System.EventArgs e)
		{
			
		}
		
		public void Timer1_Tick(object sender, System.EventArgs e)
		{
			if (MoveDown == false)
			{
				MoveY--;
				TipText.Top = MoveY;
				if (TipText.Top <= this.ClientRectangle.Height - TipText.Height)
				{
					Timer2.Enabled = true;
				}
				Timer1.Enabled = false;
			}
			else
			{
				MoveY++;
				TipText.Top = MoveY;
				if (TipText.Top >= this.ClientRectangle.Height)
				{
					Timer1.Enabled = false;
				}
			}
		}
		
		public void Timer2_Tick(object sender, System.EventArgs e)
		{
			TimCount++;
			if (TimCount > 5)
			{
				MoveDown = true;
				Timer1.Enabled = true;
				Timer2.Enabled = false;
			}
		}
	}
}
